package com.teach.servlet.admin;

import com.teach.dao.AdminInfoDAO;
import com.teach.dao.impl.AdminInfoDAOImpl;
import com.teach.entity.AdminInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * -
 * 当前的项目名：medal-2022
 *
 * @author ZT
 * @data 2022/5/8 22:10
 **/
@WebServlet("/admin/admin-update.let")
public class AdminUpdateServlet extends HttpServlet {
    private AdminInfoDAO dao = new AdminInfoDAOImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.reset();
        response.setContentType("application/json;charset=UTF-8");
        try {
            AdminInfo e = new AdminInfo();
            String adminName = request.getParameter("adminName");
            String adminPwd = request.getParameter("adminPwd");
            if (adminName == null || "".equals(adminName) || adminPwd == null || "".equals(adminPwd)) {
                throw new ServletException("管理员姓名或密码不能为空!");
            }
            e.setAdminId(Integer.valueOf(request.getParameter("adminId")));
            e.setAdminName(request.getParameter("adminName"));
            e.setAdminPwd(request.getParameter("adminPwd"));


            try {
                dao.updateById(e);
                response.getWriter().print("{\"ok\":true}");
            } catch (Exception ex) {

            }

        }catch (Exception ex){
            response.getWriter().print("{\"ok\":false,\"msg\":\""+ex.getMessage()+"\"}");
        }

        response.flushBuffer();
    }
}
